package com.ribbon.controller;

import com.ribbon.service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: RibbonController
 * @Author: ZhangYue26
 * @Description: 注意需要开启 eureka-server 模块服务
 * @Date: 2021-01-08
 */
@RestController
public class RibbonController {

    @Autowired
    private RibbonService ribbonService;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    /**
      * @Description 用于浏览器访问该接口
      * @author ZhangYue26
      * @date 2021/1/8
      * @param [name]
      * @return java.lang.String
      **/
    @GetMapping("/hi")
    public String hi(@RequestParam(required = false, defaultValue = "testname") String name) {
        return ribbonService.hi(name);
    }

    /**
      * @Description 浏览器访问hi接口，实际访问的是本接口，通过服务名调用
      * @author ZhangYue26
      * @date 2021/1/8
      * @param [name]
      * @return java.lang.String
      **/
    @GetMapping("/getName")
    public String getName(@RequestParam(required = false, defaultValue = "testname") String name) {
        return name;
    }

    @GetMapping("/testRibbon")
    public String testRibbon() {
        ServiceInstance instance = loadBalancerClient.choose("eureka-ribbon-client");
        return instance.getHost() + ":" + instance.getPort();
    }


    @GetMapping("/testMoreServices")
    public String testMoreServices() {
        return ribbonService.testMoreServices();
    }


}
